package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import metier.Imetier;
import model.Site;
import model.User;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ActionSiteadmin extends ActionSupport {
	@Autowired
	private Imetier metier;
    private Long pk;
    private String login;
	private String nom;
	private String desc;
	private String url;
	private Date date;
	private List<Site> site=new ArrayList<Site>();
	private List<Site> s=new ArrayList<Site>();
	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	
	
	
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public List<Site> getS() {
		return s;
	}
	public void setS(List<Site> s) {
		this.s = s;
	}
	public Long getPk() {
		return pk;
	}
	public void setPk(Long pk) {
		this.pk = pk;
	}
	public Imetier getMetier() {
		return metier;
	}
	public void setMetier(Imetier metier) {
		this.metier = metier;
	}
	public String getNom() {
		return nom;
	}
	public void setNom(String nom) {
		this.nom = nom;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	
public List<Site> getSite() {
		return site;
	}
	public void setSite(List<Site> site) {
		this.site = site;
	}
public String addSite(){
	User us=new User();
	us=metier.findUserByLogin(login);
	List<User> u=new ArrayList<User>();
	u.add(us);
	
	metier.addSite(new Site(nom, desc,url,new Date(),u));
	site=metier.findAllSite();
	nom="";
	desc="";
	url="";
	return "success";
}

public String findAllSite(){
	site=metier.findAllSite();
	return "success";
}
public String deleteSite(){
	
	metier.deleteSite(pk);
	site=metier.findAllSite();
	return "success";
}
public String findAllsitebyUser(){
	User u= metier.findUserByLogin((String)session.get("login"));
	
	site=u.getSites();
	return "success";
}

public String addSiteuser(){
	
	User us=new User();
	us=metier.findUserByLogin(login);
	List<User> u=new ArrayList<User>();
	u.add(us);
	
	metier.addSite(new Site(nom, desc,url,new Date(),u));
	User u1=metier.findUserByLogin(login);
	site=u1.getSites();
	nom="";
	desc="";
	url="";
	return "success";
}

public String deleteSiteuser(){
	
	metier.deleteSite(pk);
	User u1=metier.findUserById(2L);
	site=u1.getSites();
	
	return "success";
}


}
